using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Threading;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace 第五次作业
{
    public partial class Form1 : Form
    {
        List<string> urllist = new List<string>();
        List<string> emaillist = new List<string>();

        public Form1()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;                    // 防止自由生成所有数据列
            //dataGridView1.Columns[0].DataPropertyName = "phnoenum";        // 设置为某列的字段
        }

        CancellationTokenSource cancellationTokenSource =  new CancellationTokenSource(3000);
        private void button1_Click(object sender, EventArgs e)
        {
            crawpage crawpage = new crawpage();
            string url = " https://cn.bing.com/search?q=" + textBox1.Text.Trim()+ "";
            urllist = crawpage.crawUrl(url);
            Task[] tasks = new Task[urllist.Count];
            for (int i = 0; i < urllist.Count; i++)
            {
                string urll = urllist[i];
                tasks[i] = Task.Run(() =>
                        crawpage.crawEmail(urll, emaillist));
            }
            Task.WaitAll(tasks, 3000);
            dataGridView1.ColumnCount = 1;
            //设置标题行可见
            dataGridView1.ColumnHeadersVisible = true;
            //根据内容自动调整列宽
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dataGridView1.Columns[0].Name="手机号码";
            if (emaillist.Count <= 100)
            {
                for (int i = 0; i < emaillist.Count; i++)
                {
                    dataGridView1.Rows.Add(emaillist[i]);
                    //dataGridView1.Rows[i].Cells[0].Value = emaillist[i].ToString();
                }
            }
            else
            {
                for (int i = 0; i < 100; i++)
                {
                    dataGridView1.Rows.Add(emaillist[i]);
                    //dataGridView1.Rows[i].Cells[0].Value = emaillist[i].ToString();
                }
            }
        }

    }
}